function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function formatTime(date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

/**
 * 检查微信登录授权是否过期
 */
function wxCheckSession() {
  return new Promise((resolve, reject) => {
    wx.checkSession({
      success: () => resolve(true),
      fail: () => reject(false),
    })
  });
}

/**
 * 微信登录
 */
function wxLogin() {
  return new Promise((resolve, reject) => {
    wx.login({
      success: (res) => {
        if (res.code) {
          console.log(res);
          resolve(res);
        } else {
          reject(res);
        }
      },
      fail: (err) => reject(err)
    });
  })
}

function wxUserInfo() {
  return new Promise((resolve, reject) => {
    wx.getUserInfo({
      withCredentials: true,
      success: (res) => {
        console.log(res);
        resolve(res);
      },
      fail: (err) => reject(ree)
    });
  });
}

export default {
  formatTime,
  wxCheckSession,
  wxLogin,
  wxUserInfo,
}